package ncku.account.gui;

import java.awt.*;

import com.toedter.calendar.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;

import ncku.account.data.Category;
import ncku.account.data.GlobalData;
import ncku.account.data.SubTransaction;
import ncku.account.event.Launcher;

//new Transaction的介面
public class showTransaction extends JPanel {
	//介面上的元件
    
    //以JFormatedTextField實作輸入欄位
	private JLabel JLTitle;
	private JLabel JLAccount;
	private JLabel JLmoney;
	private JLabel JLTrans;
    private JComboBox categorySel;
    private JButton JBEdit;
    private JButton JBDelete;
    private tableFactory table;
    int [] indexIdList;
    
    
    String accountName;
    int balanceMoney;
    //Transaction
    //private tableFactory table;
   
    public showTransaction() {
    	accountName = GlobalData.accountName;
    	balanceMoney = GlobalData.balanceMoney;
    	
    	int indexAmount = 0;
    	for(int i = 0; i < GlobalData.getTransactionNumber(); i++){
    		indexAmount = indexAmount + 1;
    		indexAmount = indexAmount + GlobalData.getTransaction(i).getSubTransactionNumber();
    	}
    	
    	indexIdList = new int [indexAmount]; //to identify the transaction index used
		String [] categorySelItems = {"Item 1", "Item 2", "Item 3"};  //category list
		Object [] [] TransItems = new Object [indexAmount][4];  //顯示在下方Table的Transaction object
		String [] columnItems = {"交易日期","類別","交易內容", "金錢"};  //Transaction Table的項目
		
		indexAmount = 0;
		for(int i = 0; i < GlobalData.getTransactionNumber(); i++){

			TransItems [indexAmount][0] = GlobalData.getTransaction(i).getDate();
			TransItems [indexAmount][1] = GlobalData.getTransaction(i).getCategory();
			TransItems [indexAmount][2] = GlobalData.getTransaction(i).getDescription();
			TransItems [indexAmount][3] = GlobalData.getTransaction(i).getStringAmount();
			indexIdList [indexAmount] = i;
			indexAmount = indexAmount + 1;
			
			for(int j = 0; j< GlobalData.getTransaction(i).getSubTransactionNumber(); j++){
				TransItems [indexAmount][2] = GlobalData.getTransaction(i).getSubTrans(j).getSubDescription();
				TransItems [indexAmount][3] = GlobalData.getTransaction(i).getSubTrans(j).getSubStringAmount();
				indexIdList [indexAmount] = i;
				indexAmount = indexAmount + 1;
			}
		}
		
        //construct components
		JLTitle = new JLabel("交易清單");
		JLTitle.setFont(new Font("SansSerif", Font.BOLD, 25));
		JLAccount = new JLabel("帳戶名稱： " + accountName);
		JLmoney = new JLabel("帳戶餘額： " + Integer.toString(balanceMoney));
		JLTrans = new JLabel("交易明細：");
		categorySel = new JComboBox (categorySelItems);	
		JBEdit = new JButton("編輯交易");
		JBDelete = new JButton("刪除交易");
		
		table = new tableFactory(columnItems,TransItems);
        table.setOpaque(true); //content panes must be opaque
        
        JLTitle.setVisible(true);
        JLAccount.setVisible(true);
        JLmoney.setVisible(true);
        JLTrans.setVisible(true);
        JBEdit.setVisible(true);
        JBDelete.setVisible(true);

        //按下Edit後的Listener
        JBEdit.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) 
			{ 
				int index = table.table.getSelectedRow();
				if(index > -1){
					editTransaction edit = new editTransaction(indexIdList[index]);
				}
				else{
					JOptionPane.showMessageDialog(Launcher.frame, "未選擇Transaction！", "提示訊息", JOptionPane.ERROR_MESSAGE);
				}
			}}); 

        //按下Delete後的Listener
            JBDelete.addActionListener(new ActionListener() { 
    			public void actionPerformed(ActionEvent e) 
    			{ 
    				int index = table.table.getSelectedRow();
    				if(index > -1){
    					
    					//GlobalData.balanceMoney -= GlobalData.getTransaction(indexIdList[index]).getAmount();
	    				//GlobalData.transactions.remove(indexIdList[index]);
	    				GlobalData.delete(indexIdList[index]);
	    				
	    				//切換panel ，先把原本的移除掉，然後加入新的
	    				Launcher.frame.getContentPane().remove(1);
	    				Launcher.frame.getContentPane().add (BorderLayout.CENTER,new showTransaction());
	    				Launcher.frame.getContentPane().doLayout();
    				}
    				else{
    					JOptionPane.showMessageDialog(Launcher.frame, "未選擇Transaction！", "提示訊息", JOptionPane.ERROR_MESSAGE);
    				}	
    			}}); 
        
        //adjust size and set layout
        setPreferredSize (new Dimension (681, 422));
        setLayout (null);

        //add components
        add(JLTitle);
        add(JLAccount);
        add(JLmoney);
        add(categorySel);  //不知名bug,不add這個物件,table會莫名其妙跑不出來
        add(JLTrans);
        add(JBEdit);
        add(JBDelete);
        
        add(table);

        //setBounds(50, 125, 80, 30); //(X座標,Y座標,元件寬,元件高)
        //set component bounds (only needed by Absolute Positioning)
        JLTitle.setBounds(240,20,200,30);
        JLAccount.setBounds(120,50,150,30);
        JLmoney.setBounds(300,50,150,30);
        JLTrans.setBounds(50, 90, 100, 30);
        JBEdit.setBounds(150,90,80,30);
        JBDelete.setBounds(250,90,80,30);
        table.setBounds(40,120,520,160);
 
    }
}
